package mysql

import (
	"gitee.com/dreamwood/ez"
	"io/ioutil"
	"strings"
)

func CreateVueAdminFile(creator *Creator) {
	path := root + "/twig/tpl/maker/vue/admin"
	target := root + "/var/output/"
	files, err := ioutil.ReadDir(path)
	if err != nil {
		ez.JsonLog(err)
	}
	for _, file := range files {
		if file.Name() == "js" {
			continue
		}
		content, _ := ioutil.ReadFile(path + "/" + file.Name())
		tpl := string(content)
		tpl = strings.ReplaceAll(tpl, "__DIR__", creator.Table.Path)
		tpl = strings.ReplaceAll(tpl, "__EN_NAME__", creator.Table.Name)
		//默认配置信息输入
		ez.CreateDirForPath(target + creator.Table.Path + "/" + creator.Table.Name + "/" + file.Name())
		err := ioutil.WriteFile(target+creator.Table.Path+"/"+creator.Table.Name+"/"+file.Name(), []byte(tpl), 0777)
		if err != nil {
			ez.JsonLog(err)
		}
	}
	path = root + "/twig/tpl/maker/vue/admin/js"
	target = root + "/var/output/"
	files, err = ioutil.ReadDir(path)
	if err != nil {
		ez.JsonLog(err)
	}
	for _, file := range files {
		content, _ := ioutil.ReadFile(path + "/" + file.Name())
		tpl := string(content)
		tpl = strings.ReplaceAll(tpl, "__DIR__", creator.Table.Path)
		tpl = strings.ReplaceAll(tpl, "__EN_NAME__", creator.Table.Name)
		//默认配置信息输入
		ez.CreateDirForPath(target + creator.Table.Path + "/" + creator.Table.Name + "/js/" + file.Name())
		err := ioutil.WriteFile(target+creator.Table.Path+"/"+creator.Table.Name+"/js/"+file.Name(), []byte(tpl), 0777)
		if err != nil {
			ez.JsonLog(err)
		}
	}
	ez.PrintSuccess("后台代码生成成功", creator.Table.CnName, creator.Table.Name)
	//ez.JsonLog(files)
}
